<template>
	<view class="">
		<view class="goods-item" v-for="(goods,index) in goodsList" v-if="goods.num>0" :key="index">
			<!-- 商品左侧图片区域 -->
			<view class="goods-item-left">
				<!-- 添加 radio 1： 添加组件 -->
				<!-- checked 选中与否的状态 -->
				<!-- 添加 radio 4: 通过 v-if 决定是否渲染 radio -->
				<radio color="#c00000" @click="radioHandler" :checked="isChecked" v-if="showRadio" />
				<!-- 失效文字 -->
				<view v-if="showDisabledGoods" class="disabled-text">失效</view>
				<image :src="goods.imgUrl" class="goods-pic"></image>
			</view>
			<!-- 商品右侧信息区域 -->
			<view class="goods-item-right">
				<!-- 商品标题 -->
				<view class="">
					<view class="goods-info-box">
						<view class="disabled-goods-name" v-if="showDisabledGoods">{{goods.productName}}</view>
						<view class="goods-name" v-else>{{goods.productName}}</view>
		
						<view class="price-num">
							<!-- 商品价格 -->
							<view class="diabled-goods-price" v-if="showDisabledGoods">￥{{goods.price}}</view>
							<view class="goods-price" v-else>￥{{goods.price}}</view>
							<view class="remove-goods" v-if="showDisabledGoods">
								删除
							</view>
							<!-- 订单页面，商品数量 -->
							<view class="goods-num" v-if="showGoodsNum">
								×{{goods.num}}
							</view>
						</view>
					</view>
				</view>
		
		
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		props: {
			// 组件使用者传递的商品的数据对象
			// goods: {
			// 	type: Object,
			// 	default: {}
			// },
			// 商品详情列表
			goodsList:{
				type:Array,
				default:[]
			},
			// <!-- 添加 radio 3： 定义是否展示的 props -->
			// 控制单选框是否需要进行展示
			showRadio: {
				type: Boolean,
				default: false
			},
			// 控制商品数量加减组件的展示
			showNum: {
				type: Boolean,
				default: false
			},
			// 控制失效商品列表的展示
			showDisabledGoods: {
				type: Boolean,
				default: false
			},
			// 控制订单页面商品数量显示
			showGoodsNum: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				status: false,
				isChecked: false,
				// 默认的空图片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				num: 10
			}
		},
		methods: {
			// 添加商品到购物车
			async addAmount() {
				const {
					data: res
				} = await uni.$http.post('/cart/addCart', {
					userId: this.userInfo._id,
					productId: this.goods._id,
					num: '1'
				})
				console.log(res)
				this.$emit('num-change')
			},
			// 商品减少事件
			substactAmount() {},
			// 向组件使用者提供商品数量加减的事件(需要传递 id 和 最新的数据 )
			numChangeHandle(value, Onum) {
				console.log(value, Onum)
				this.$emit('num-change', {
					count: +value,
					Onum: Onum
				})
			},

			// 向组件使用者提供一个选中与否的切换事件
			radioHandler() {
				this.isChecked = !this.isChecked
				
			}
		},
		computed: {
			...mapState('m_user', ['userInfo'])
		},
		filters: {
			// 给价格添加两位小数点
			toFixed(price) {
				// toFixed 方法转成带两位小数点的数字
				return Number(price).toFixed(2)
			}
		}
	}
</script>

<style lang="scss">
	.goods-num {
		font-size: 30rpx;
		font-weight: 400;
		color: #666666;
		position: absolute;
		right: 24rpx;
		
	}

	.price-num {
		display: flex;
		justify-content: space-between;
	}

	.remove-goods {
		// display: flex;
		// align-items: center;
		// text-align: right;
		// margin-left: 110rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: #333333;
	}

	.diabled-goods-price {
		color: #999999;
		font-size: 16px;
		width: 200rpx;
		margin-right: 200rpx;
	}

	.goods-price {
		font-size: 16px;
		color: #c00000;
		width: 200rpx;
	}

	.disabled-goods-name {
		font-size: 30rpx;
		font-weight: 500;
		color: #999999;
		margin-top: 22rpx;
		// margin-bottom: 55rpx;
		height: 80rpx;
	}

	.disabled-text {
		width: 59rpx;
		height: 30rpx;
		line-height: 30rpx;
		background: #ACACAC;
		border-radius: 15rpx;
		text-align: center;
		font-size: 20rpx;
		font-weight: 500;
		color: #FFFFFF;
	}

	.goods-item {
		display: flex;
		padding: 34rpx 24rpx;
		border-bottom: 1px solid #f0f0f0;
		position: relative;

		.goods-item-left {
			// <!-- 添加 radio 2： 设置样式 -->
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-right: 20rpx;

			.goods-pic {
				width: 140rpx;
				height: 140rpx;
				border-radius: 12rpx;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;

			.goods-name {
				font-size: 30rpx;
				font-weight: 500;
				color: #333;
				margin-top: 22rpx;
				// margin-bottom: 20rpx;
				height: 80rpx;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.goods-info-box {
				display: flex;
				flex-direction: column;
				// width: 300rpx;
				// justify-content: space-between;
				// align-items: center;
			}


		}

		.num-box {
			height: 160rpx;
			display: flex;
			align-items: center;
		}

		.plus-substact {
			width: 172rpx;
			height: 52rpx;
			border: 1rpx solid #D3D3D3;
			border-radius: 26rpx;
			display: flex;
			justify-content: space-evenly;
			align-items: center;
			text-align: center;
			margin-left: 80rpx;

			.substact,
			.plus,
			.amount {
				flex: 1;
				font-size: 40rpx;
				height: 100%;
			}

			.amount {
				border-left: 1px solid #D6D6D6;
				border-right: 1px solid #D6D6D6;
				font-size: 34rpx !important;
				font-weight: 500;
				color: #5B5B5B;
			}
		}
	}
</style>
